<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/commons/pages/taglibs.jsp" %>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>角色管理</title>
<link rel="stylesheet" type="text/css" href="../jquery-easyui/themes/${themes}/easyui.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="../styles/demo.css">
<script type="text/javascript" src="../jquery-easyui/jquery.min.js"></script>
<script type="text/javascript" src="../jquery-easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../scripts/public.js.jsp"></script>
<script type="text/javascript" src="${ctx}/platform/type-info/format"></script>
</head>
<body>
<!--div class="demo-info">
  <div class="demo-tip icon-tip"></div>
  <div>角色管理</div>
</div-->
<c:if test="${fn:containsIgnoreCase(action_codes, 'S')}">
  <div style="padding:5px; margin-bottom:5px; display:block;" class="combo">
    <form method="post" id="fm_search" novalidate>
      名称：
      <input class="easyui-validatebox" style="width:80px;" id="role_name" name="role_name" />
      锁定：
      <select id="is_lock" name="is_lock" style="width:80px;">
      </select>
      删除：
      <select id="is_del" name="is_del" style="width:50px;">
      </select>
      <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="doSearch();">搜索</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="$('#fm_search').form('clear');">重置</a>
    </form>
  </div>
</c:if>
<div id="dg"> </div>
<div id="dlg" class="easyui-dialog" data-options="closed:true, modal:true, buttons:'#dlg-buttons'" style="width:400px; height:280px; padding:10px 40px">
  <form method="post" id="fm" novalidate>
    <input name="id" type="hidden" id="id" />
    <table>
      <tr>
        <td align="right"><b>角色代码：</b></td>
        <td><select id="role_code" name="role_code" style="width:60px;" class="easyui-validatebox" data-options="required:true">
          </select></td>
      </tr>
      <tr>
        <td align="right"><b>角色名称：</b></td>
        <td><input id="role_name" name="role_name" style="width:200px;" type="text" class="easyui-validatebox" data-options="required:true" /></td>
      </tr>
      <tr>
        <td align="right"><b>角色说明：</b></td>
        <td><input id="role_desc" name="role_desc" style="width:200px;" type="text" /></td>
      </tr>
      <tr>
        <td align="right"><b>排序值：</b></td>
        <td><input id="order_value" name="order_value" style="width:80px;" class="easyui-numberspinner" min="0" max="9999" value="0" /></td>
      </tr>
      <tr>
        <td align="right"><b>锁定：</b></td>
        <td><select id="is_lock" name="is_lock" style="width:80px;" class="easyui-validatebox" data-options="required:true">
          </select></td>
      </tr>
      <tr>
        <td align="right"><b>删除：</b></td>
        <td><select id="is_del" name="is_del" style="width:80px;" class="easyui-validatebox" data-options="required:true">
          </select></td>
      </tr>
    </table>
  </form>
</div>
<div id="dlg-buttons"> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="doSave();">保存</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="$('#dlg').dialog('close');">关闭</a> </div>
<!-- 角色授权对话框开始 -->
<div id="dlg-grant" class="easyui-dialog"  data-options="closed:true, modal:true, buttons:'#dlg-grant-buttons', maximizable:true" style="width:800px; height:480px; padding:5px;">
  <form method="post" id="fm-grant" novalidate>
    <input type="hidden" id="role_code" name="role_code" />
    <div id="dg-grant"> </div>
  </form>
</div>
<div id="dlg-grant-buttons"><a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="$('#btnCheckAll').click();">全部操作</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="$('#btnSaveGrant').click();">保存授权</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="$('#btnCloseGrantDlg').click();">关闭</a> </div>
<!-- 角色授权对话框结束--> 
<script type="text/javascript">
$(function(){
	$('#fm #role_code').combobox({  
	    valueField: 'value',  
	    textField: 'text'
	}).combobox('loadData', code_data);
	
	//初始化列表
	$('#dg').datagrid({  
	    url:'${ctx}/platform/role-info/search',
	    queryParams:$('#fm_search').serializeObject(),
	    frozenColumns:[[
	    	{field:'ck',checkbox:true}
	 	]],
	    columns:[[
			//{field:'id',title:'id',hidden:true},
			{field:'role_code',title:'角色代码'/*,width:60*/,align:'center'},
	        {field:'role_name',title:'角色名称'/*,width:100*/},
			{field:'role_desc',title:'角色说明'/*,width:400*/},
			{field:'id',title:'为角色授权'/*,width:100*/,align:'center',formatter:function(value, row, index){
				return '<span style="cursor:pointer;" onclick="doGrantAdd(\'' + row.role_code + '\', \'' + row.role_name + '\')">为角色[ ' + row.role_code + ' ]授权</span>';
			}},
			{field:'order_value',title:'排序值'/*,width:50*/,align:'right'},
			{field:'is_lock',title:'锁定'/*,width:40*/,align:'center',formatter:formatBoolean,styler:styleBoolean},
			{field:'is_del',title:'删除'/*,width:40*/,align:'center',formatter:formatBoolean,styler:styleBoolean}
	    ]],
		//fitColumns:true,
		striped:true,
		pagination:true,
		remoteSort:false,
		rownumbers:true,
		singleSelect:false,
		toolbar:[
			//<c:if test="${fn:containsIgnoreCase(action_codes, 'D')}">
			{
				id:'btnRemove',
				text:'删除',
				iconCls:'icon-remove',
				handler:function(){doRemove();}
			},'-',
			//</c:if>
			//<c:if test="${fn:containsIgnoreCase(action_codes, 'C')}">
			{
				id:'btnAdd',
				text:'添加',
				iconCls:'icon-add',
				handler:function(){doAdd();$('#fm #role_code').removeProp('readOnly').css({color:"#000"});}
			},
			//</c:if>
			//<c:if test="${fn:containsIgnoreCase(action_codes, 'U')}">
			{
				id:'btnEdit',
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){doEdit();$('#fm #role_code').prop('readOnly', true).css({color:"#ccc"});}
			},
			//</c:if>{}
		]
	}); 
	
	$('#dg-grant').treegrid({  
	    url:'${ctx}/platform/sys-module/search',
	    idField:'id',  
	    treeField:'mod_name',
	    columns:[[
			{field:'mod_icon',title:'图标',width:40,align:'center',formatter:formatActionIcon},
			{field:'mod_name',title:'名称',width:200},
	        //{field:'mod_code',title:'编码',width:40,align:'right'},
	        //{field:'mod_url',title:'URL',width:120},
			{field:'is_public',title:'公共',width:40,align:'center',formatter:formatBoolean,styler:styleBoolean},
			{field:'action_codes',title:'可授权的操作',width:240,formatter:function(value, row, index){
				if (row.is_public == 1) return '<span style="color:#666">公共模块，无需授权</span>';
				if (row.is_del == 1) return '<span style="color:#666">已删除模块，授权无意义</span>';
				var results = [];
				var action_codes = value.split(',');
				for (var i = 0; i < action_codes.length; i++) {
					//var ids = []; ids.push('m', row.mod_code, action_codes[i]); var id = ids.join("_");
					var id = 'm_' + row.mod_code + '_' + action_codes[i];
					results.push('<input type="checkbox" id="', id, '" name="', id, '" /><label for="', id, '">', action_codes[i], '</label>');
				}
				return results.join('');
			}},
			{field:'id',title:'<input type="checkbox" id="m_all" onclick="check(this, \'m_\')" /><label for="m_all">全选</label>'/*,width:60*/,formatter:function(value, row){
				var id_prefixs = []; id_prefixs.push('m', row.mod_code, ''); var id_prefix = id_prefixs.join("_");
				var results = []; results.push('<input type="checkbox" id="', id_prefix, '" onclick="check(this, \'', id_prefix, '\')"/><label for="', id_prefix, '">行选</label>');
				return results.join('');
			}},
			{field:'is_lock',title:'锁定',width:40,align:'center',formatter:formatBoolean,styler:styleBoolean},
			{field:'is_del',title:'删除',width:40,align:'center',formatter:formatBoolean,styler:styleBoolean}
	    ]],
		striped:true,
		remoteSort:false,
		rownumbers:true,
		singleSelect:false,
		toolbar:[{
			id:'btnCheckAll',
			text:'全部操作',
			iconCls:'icon-ok',
			handler:function(){check($('#m_all').get(0), 'm_', true);}
		},{
			id:'btnSaveGrant',
			text:'保存授权',
			iconCls:'icon-save',
			handler:function(){doGrantSave();}
		},{
			id:'btnCloseGrantDlg',
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){$('#dlg-grant').dialog('close');}
		}] 
	});

});

//保存授权
function doGrantSave(cfg){
	$('#fm-grant').form('submit', {
		url: '${ctx}/platform/sys-mod-action/save',
	    onSubmit: function(){
	       if ($('input[id^="m_"]:checked').length < 1) {
	   			$.messager.alert(MSG_WARNING, '没有选择任何操作！', 'warning');
				return false;
	       }
	    },  
	    success: function(result){
	    	var result = eval('(' + result + ')');
			if (result.success){
				$.messager.show({title:MSG_INFO, timeout:3000, msg:'授权成功！'});
			} else {
			    $.messager.show({title:MSG_ERROR, timeout:3000, msg:result.msg});
			}
			$('#dlg-grant').dialog('close');
	    }
	});
}

//显示授权对话框并回显已经授权的操作
function doGrantAdd(role_code, role_name){
	if (!role_code) {
		$.messager.alert('错误','没有设定角色代码！','error');
		return false;
	}
	$('#fm-grant #role_code').val(role_code);
	$('#dlg-grant').dialog({'maximized':true}).dialog('open').dialog('setTitle', '给角色（' + role_name + '[' + role_code + ']）授权');
	
	//回显此角色代码已经授权的操作
	$.post('${ctx}/platform/sys-mod-action/json', {role_code:role_code}, function(data){
		//alert(JSON.stringify(data));
	 	for (var i = 0; i < data.length; i++) {
	 		var cur = data[i];
	 		var id = 'm_' + cur.mod_code + "_" + cur.action_code;
	 		//$('"#' + id + '"').prop("checked", true);//这个为什么不行呢？！
	 		$('input[id="' + id + '"]').prop("checked", true);
	 	}
	}, 'json');
}

//行选
function check(e, id_prefix, reverse){
	var checked_val = $(e).prop("checked"); // true|false, do not use attr()
	$('input[id^="' + id_prefix + '"]').prop("checked", reverse ? !checked_val : checked_val);
}
</script>
</body>
</html>